﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_register1 : System.Web.UI.Page
{
    DataClassesDataContext db1 = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack == true)
        {
            var resultStu = (from p in db1.proc_GetRegisterStuInfo()
                             select p).ToList();
            GridView2.DataSource = resultStu;
            GridView2.DataBind();
            string IP = Request.ServerVariables.Get("Remote_Addr").ToString();
            string nowtime = DateTime.Now.ToString();
            DateTime nowtime1 = Convert.ToDateTime(nowtime);
            int nowusername = Convert.ToInt32(Session["username"]);
            var newrizhi = new rizhi
            {
                userName = nowusername,
                time = nowtime1,
                ip = IP,
                caozuo = "进入注册管理"
            };
            db1.rizhi.InsertOnSubmit(newrizhi);
            db1.SubmitChanges();
        }
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.Cells[1].FindControl("Label2");
            lbl.Text = (GridView2.PageIndex * GridView2.PageSize + e.Row.RowIndex + 1).ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var resultStu = (from p in db1.proc_GetRegisterStuInfo()
                         where p.wbh.Contains(TextBox1.Text) || p.sname.Contains(TextBox1.Text)
                         select p).ToList();
        GridView2.DataSource = resultStu;
        GridView2.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        bool flag=true;
        foreach (GridViewRow Row in GridView2.Rows)
           {
                try
                {
                    if (((CheckBox)Row.FindControl("CheckBox1")).Checked == true)
                    {
                        string wbh = ((Label)Row.FindControl("Label1")).Text;
                        if (db1.proc_ResetStuPwd(wbh) == -1)
                        {
                            flag = false;
                        }
                    }
                 }
                catch (Exception ex)
                {
                    return;
                }
            }
            if (flag == false)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "message", "<script>alert('密码重置失败，请重试！');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script>alert('密码已初始化为身份证的后六位！');</script>");
            }
      }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 得到该控件
        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (e.NewPageIndex == -3)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        //得到新的值
        theGrid.PageIndex = newPageIndex;

        GridView2.DataSourceID = "";
        var resultStu = (from p in db1.proc_GetRegisterStuInfo()
                         select p).ToList();
        GridView2.DataSource = resultStu;
        GridView2.DataBind();
    }
}
